package com.example.contactlist;



import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter{

	private Context con=null;
	private ArrayList<Contact> data=null;
	private int res=0;

	public ListAdapter(Context con, ArrayList<Contact> data, int res) {
		super();
		this.con = con;
		this.data = data;
		this.res = res;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		Contact item = (Contact)getItem(position);
		return item.getId();
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Activity act =(Activity)con;
		Contact item =(Contact)getItem(position);
		RelativeLayout v =(RelativeLayout)act.getLayoutInflater().inflate(res, null);
//		String img =item.get("img");
//		Resources res = con.getResources();
//		int resId = res.getIdentifier(img, "drawable", con.getPackageName());
//		((ImageView)v.findViewById(R.id.img)).setImageResource(resId);
		((TextView)v.findViewById(R.id.nameText)).setText(item.getName());

		return v;
	}
	public void add(Contact contact){
		this.data.add(contact);
		this.notifyDataSetChanged();
	}
	public void delete(Contact contact){
		this.data.remove(contact);
		this.notifyDataSetChanged();
	}
	public void clear(){
		this.data.clear();
		this.notifyDataSetChanged(); //データが変わったことを知らせる
	}
}

